package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.YddAdvimg;
import com.itheima.youdianda.service.YddAdvimgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {

    @Autowired
    private YddAdvimgService yddAdvimgService;

    //广告图分页查询
    @GetMapping("/list")
    public R<Page> list(String advimgdesc, String advimgpos, int page, int limit){
        log.info("advimgdesc={},advimgpos={},page={},limit={}",advimgdesc,advimgpos,page,limit);
        //构造分页构造器
        Page pageInfo = new Page(page,limit);
        //构造条件构造器,添加过滤条件
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc);
        queryWrapper.eq(StringUtils.isNotEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);
        //yddAdvimgService.page(pageInfo,queryWrapper);
        //执行查询
        return R.success(pageInfo);
    }

    //回显广告图详情
    @GetMapping("/rowInfo")
    public R<YddAdvimg> rowInfo(int id){
        log.info("id为:{}",id);
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return R.success(yddAdvimg);
    }

    //广告图修改
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg){
        log.info("修改信息为:{}",yddAdvimg.toString());
        yddAdvimgService.save(yddAdvimg);
        return R.success("修改数据成功!");
    }

    //广告图删除
    @GetMapping("/del")
    public R<String> del(int id){
        log.info("要删除的id为:{}",id);
        yddAdvimgService.removeById(id);
        return R.success("删除成功!");
    }

    //广告图添加
    @GetMapping("/add")
    public R<String> add(YddAdvimg yddAdvimg){
        log.info("要添加的广告图信息为:{}",yddAdvimg.toString());
        yddAdvimgService.save(yddAdvimg);
        return R.success("添加成功!");
    }
}
